home *** CD-ROM | disk | FTP | other *** search
- %!PS-Adobe-3.0 Resource-ProcSet
- %%Title: PSImageProlog
- %%Copyright: 1993 Silicon Graphics, Inc. All rights reserved.
- %
- % Procedure: processImage
- %
- % Description:
- % This PostScript code prolog defines a procedure for displaying
- % a bitmap image. The procedure provides for zooming, rotating
- % and mirroring the image. In addition, the procedure accepts
- % both ASCII hex image data and binary image data.
- %
- % Syntax:
- % IWidth IHeight IBits Binary NChans Angle BestFit FlipY RasDir
- % Zoom IRes NUpx NUpy NPos processImage
- %
- % Parameters:
- % IWidth, IHeight - Image width and height in pixels
- % IBits - Bits per color component (1, 2, 4, or 8)
- % Binary - Data type: 0 = ASCII hex, 1 = binary
- % NChans - Number of data channels (1, 3 or 4)
- % Angle - Image rotation angle in degress
- % BestFit - Image orientation: 0 = use angle, 1 = best fit
- % FlipY - Mirror image about Y axis: 0 = no, 1 = flip
- % RasDir - Raster direction: 0 = top to bottom, 1 = bot. to top
- % Zoom - Zoom factor: 0.0 = no zoom, 1.0 = zoom to fit
- % imageable area, 0.5 = zoom to 50% of imageable area
- % IRes - Render image assuming specified resolution
- % NUpx, NUpy - Place nupx by nupy images per page
- % NPos - Position in nup sequence (0, 1, ..., nup-1)
- %
- %%BeginResource: procset processImage 1.2 0
-
- /processImage {
-
- % Get procedure parameters
-
- /NPos exch def % N up position
- /NUpY exch def % N up in Y direction
- /NUpX exch def % N up in X direction
- /IRes exch def % Required image resolution
- /Zoom exch def % Zoom factor
- /RasDir exch def % Raster direction
- /FlipY exch def % Mirror about Y
- /BestFit exch def % Page orientation flag
- /Angle exch def % Rotation angle (degrees)
- /NChans exch def % Number of image data channels
- /Binary exch def % Binary data flag
- /IBits exch def % Bits per component
- /IHeight exch def % Image height
- /IWidth exch def % Image width
-
- % Get the frame buffer dimensions
-
- newpath clippath pathbbox
- /URy exch def
- /URx exch def
- /LLy exch def
- /LLx exch def
- /FBWidth URx LLx sub def
- /FBHeight URy LLy sub def
-
- % If we are doing N up calculate new frame buffer dims and
- % image origin coords
-
- NUpX 1 gt NUpY 1 gt or {
- /MarX 10 def % Inter-image margins
- /MarY 10 def
- /FBWidth FBWidth NUpX 1 add MarX mul sub NUpX div def
- /FBHeight FBHeight NUpY 1 add MarY mul sub NUpY div def
- /LLx MarX FBWidth add NPos NUpX mod mul MarX add LLx add def
- /LLy NUpY NPos NUpX NUpY mul mod NUpX idiv sub 1 sub
- MarY FBHeight add mul MarY add LLy add def
- } if
-
- % Compute aspect ratios
-
- /FBAspect FBWidth FBHeight div def
- /IAspect IWidth IHeight div def
-
- % If best fit desired, determine best fit rotation angle.
- % The best fit occurs when the image and frame buffer aspect
- % ratios are both on the same side of 1.0
-
- BestFit 1 eq {
- FBAspect 1.0 lt IAspect 1.0 lt xor {
- /Angle 90 def
- } {
- /Angle 0 def
- } ifelse
- }
-
- % Otherwise normalize the rotation angle to 0 <= Angle < 360
- % so that we can perform bounding box calc and we only have
- % to special case a few angles
-
- {
- /Angle Angle 360 mod dup 0 lt {
- 360 add
- } if def
- } ifelse
-
- % Compute the bounding box dimensions of the rotated image
- % and the bounding box aspect ratio.
-
- Angle 0 eq Angle 180 eq or {
- /IBWidth IWidth def
- /IBHeight IHeight def
- /IBAspect IAspect def
- } {
- Angle 90 eq Angle 270 eq or {
- /IBWidth IHeight def
- /IBHeight IWidth def
- /IBAspect IHeight IWidth div def
- } {
- /AbsSin Angle sin abs def
- /AbsCos Angle cos abs def
- /IBWidth IWidth AbsCos mul IHeight AbsSin mul add def
- /IBHeight IWidth AbsSin mul IHeight AbsCos mul add def
- /IBAspect IBWidth IBHeight div def
- } ifelse
- } ifelse
-
- % Calculate the scaled image bounding box dimensions. This is
- % also where we apply the zoom factor or ppi scaling.
-
- IRes 0 eq {
- Zoom 0.0 gt { % Zoom as fraction imageable area
- FBAspect IBAspect lt {
- /XBSize FBWidth Zoom mul def
- /YBSize FBWidth IBAspect div Zoom mul def
- } {
- /XBSize FBHeight IBAspect mul Zoom mul def
- /YBSize FBHeight Zoom mul def
- } ifelse
- } { % No zoom, 1 dot in == 1 dot out
- /XBSize IBWidth 1 1 dtransform pop abs div def
- /YBSize IBHeight 1 1 dtransform exch pop abs div def
- } ifelse
- } { % Required image resolution (ppi)
- /XBSize IBWidth IRes 72 div div def
- /YBSize IBHeight IRes 72 div div def
- } ifelse
-
- % Calculate the scaled image dimensions
-
- Angle 0 eq Angle 180 eq or {
- /XISize XBSize def
- /YISize YBSize def
- } {
- Angle 90 eq Angle 270 eq or {
- /XISize YBSize def
- /YISize XBSize def
- } {
- /XISize XBSize AbsCos 1.0 IAspect div AbsSin mul add div def
- /YISize XBSize IAspect AbsCos mul AbsSin add div def
- } ifelse
- } ifelse
-
- % Translate origin of imageable area
-
- LLx LLy translate
-
- % Center the image
-
- FBWidth XBSize sub 2 div
- FBHeight YBSize sub 2 div
- translate
-
- % Rotate the image about its center, then mirror it
- % about y if flip is specified
-
- XBSize 2 div YBSize 2 div translate
- FlipY 1 eq {
- 1.0 neg 1.0 scale
- } if
- Angle rotate
- XISize 2 div neg YISize 2 div neg translate
-
- % Scale the image to the desired size
-
- XISize YISize scale
-
- % Define a data reading procedure appropriate to
- % the image data type
-
- Binary 0 eq {
- /getByte { readhexstring } bind def
- } {
- /getByte { readstring } bind def
- } ifelse
-
- % Execute the sampling operator appropriate to the
- % colorspace of the data. That is, 'image' if number of
- % channels is 1 (e.g. W), 'colorimage' if it is 3 (e.g. RGB)
- % or 4 (e.g. CMYK)
- %
- % Even though the Redbook indicates the string can be any size
- % experience indicates that string should be the number of
- % bytes per row.
-
- /IRowStr NChans IWidth mul IBits
- mul 7 add 8 idiv string def % Allocate row bytes of string
- IWidth IHeight IBits % Image width, height, bpp
- RasDir 0 eq { % Sample based on raster direction
- [IWidth 0 0 IHeight neg 0 IHeight] % Top to bottom
- } {
- [IWidth 0 0 IHeight 0 0] % Bottom to top
- } ifelse
- { currentfile IRowStr getByte pop } bind % Sampling proc
- NChans 1 eq { % Use image if black and white
- image
- } {
- systemdict /colorimage known { % Only if have colorimage
- false NChans % Interleaved data
- colorimage
- } { % If no colorimage print error
- initgraphics
- /Courier findfont 12 scalefont setfont
- LLx 5 add FBHeight 2 div 2 copy moveto
- (PSImageProlog Error: 'colorimage' operator not available,)
- show
- 15 sub moveto
- ( re-render image in black and white \(w\).)
- show
- showpage
- quit
- } ifelse
- } ifelse
- } bind def
-
- %%EndResource
-